package com.web.server.controller.vote;

import com.web.server.common.response.Response;
import com.web.server.entity.vote.Vote;
import com.web.server.service.vote.VoteService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by HUJING3 on 2016/12/16.
 */
@Controller
@RequestMapping("/vote")
public class VoteController {
    private static final Logger RUN_LOG = Logger.getLogger(VoteController.class);

    @Autowired
    VoteService voteService;

    @ResponseBody
    @RequestMapping("/submit")
    public Response submit(Vote vote, HttpServletRequest request) {
        RUN_LOG.info("vote submit form:" + request.getRemoteAddr());
        Response response = new Response();
        voteService.insert(vote);
        response.setData("ok");
        return response;
    }

    @ResponseBody
    @RequestMapping("/list")
    public Response list(Integer pageNo, Integer pageSize, HttpServletRequest request) {
        RUN_LOG.info("vote list form:" + request.getRemoteAddr());
        if (pageNo == null || pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        Response response = new Response();
        response.setData(voteService.list(pageNo, pageSize));
        return response;
    }
}
